import { Injectable } from '@nestjs/common';
// import { CreateGpuDto } from './dto/create-gpu.dto';
// import { UpdateGpuDto } from './dto/update-gpu.dto';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { GpuCodeDocumentType } from '../schema/GPU/gpu_code.schema';
import { GPUDocumentType } from '../schema/GPU/gpu.schema';

import { GpuType } from '../type/gpu_type';

@Injectable()
export class GpuService {
  //注册显卡芯片的Schema、这是一个私有属性，它将被注入一个 Mongoose 模型实例。
  constructor(
    @InjectModel('GPU_CODE') private GpuCodeModel: Model<GpuCodeDocumentType>,
    @InjectModel('GPU') private GpuModel: Model<GPUDocumentType>,
  ) {}

  //添加显卡芯片数据
  async createGpuCode(code_name: string, manufacturers: string) {
    const gpu_code = new this.GpuCodeModel({ code_name, manufacturers });
    console.log(gpu_code);

    return gpu_code.save();
  }

  //查询显卡芯片数据
  async findGpuCodeByManufacturers(manufacturers: string) {
    const result = await this.GpuCodeModel.find({ manufacturers }).exec();

    return result;
  }

  //上传显卡信息
  async createGpuInfo(body: GpuType) {
    const gpu = new this.GpuModel(body);
    return gpu.save();
  }

  //查询显卡信息
  async getGpuInfo(query: any) {
    const { page, limit, code, gpuCapacity, order, price } = query;

    let sort = {};
    if (order) {
      sort = { price: parseInt(order) };
    } else {
      sort = {};
    }

    // 构建查询条件
    const filter: any = {};
    if (code) {
      filter.code = code;
    }
    if (gpuCapacity) {
      filter.gpuCapacity = gpuCapacity;
    }
    if (price) {
      if (price.min && !price.max) {
        filter.price = {
          $gte: parseFloat(price.min),
        };
      } else if (price.max && !price.min) {
        filter.price = {
          $lte: parseFloat(price.max),
        };
      } else {
        filter.price = {
          $gte: parseFloat(price.min),
          $lte: parseFloat(price.max),
        };
      }
    }

    //执行查询
    const result = await this.GpuModel.find(filter)
      .skip((page - 1) * limit)
      .limit(limit)
      .sort(sort)
      .exec();

    return {
      code: 200,
      msg: 'success',
      data: {
        result,
        total: await this.GpuModel.countDocuments(filter),
      },
    };
  }

  //根据id查询显卡信息
  async getGpuInfoById(id: string) {
    const result = await this.GpuModel.findById(id).exec();
    return {
      code: 200,
      msg: 'success',
      data: result,
    };
  }

  //更新显卡信息
  async updateGpuInfo(id: string, body: any) {
    const result = await this.GpuModel.findByIdAndUpdate(id, body, {
      new: true,
    }).exec();

    return {
      code: 200,
      msg: 'success',
      data: result,
    };
  }

  //删除显卡信息
  async deleteGpuInfo(id: string) {
    const result = await this.GpuModel.findByIdAndDelete(id).exec();

    return {
      code: 200,
      msg: 'success',
      data: result,
    };
  }

  //请求所有显卡信息
  async getTotalGpuInfo() {
    const result = await this.GpuModel.find().select('price score id').exec();

    return {
      code: 200,
      msg: 'success',
      data: result,
    };
  }
}
